﻿using GraduationProject.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GraduationProject.Webs.Areas.Admin.Controllers
{
    /// <summary>
    /// 文章发布控制器
    /// </summary>
    public class AdminNovelController : AdminBaseController
    {
        #region 字段
        public readonly GraduationProject.RDBMS.Novel _novelService = new RDBMS.Novel();
        #endregion
        // GET: Admin/AdminNovel
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 分页获取文章数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetNovelByPage(int page, int limit)
        {
            Novel[] Novels;
            int TotalCount;
            using (var conn = new RDBMS.Novel())
            {
                if (conn.TrySelectNovelByPage(out Novels, out TotalCount, page, limit,IsReview:false))
                {
                    return new NewtonsoftJson(new { code = 0, msg = "成功", count = TotalCount, data = Novels });
                }
            }
            return Json(new { code = 1, msg = "抱歉,暂无数据", count = 0, data = new List<Novel>() });
        }
        /// <summary>
        /// 修改文章操作
        /// </summary>
        /// <param name="NovelID"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult UpdatePage(int NovelID)
        {
            SortList[] Sort;
            //获取分类标记数据
            using (var conn = new RDBMS.SortList())
            {
                if (conn.TrySelectSortList(out Sort, null))
                {
                    ViewData["SortModel"] = Sort.ToList();
                }
            }
            //根据ID查询文章数据
            Novel[] novel;
            //获取分类标记数据
            using (var conn = new RDBMS.Novel())
            {
                if (conn.TrySelectNovel(out novel, new int[] { NovelID }))
                {
                    ViewData["NovelModel"] = novel.ToList().FirstOrDefault();
                }
            }
            return View();
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult AdminUpdateNovel(Novel model)
        {

            var models = new Novel[] { model };
            using (var conn = new RDBMS.Novel())
            {
                if (conn.TryUpdateNovel(out bool result, models))
                {
                    if (result)
                    {
                        return Json(new { code = 0, msg = "修改成功" });
                    }
                }
            }
            return Json(new { code = 1, msg = "修改失败，请稍后重试！" });
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AdminUploadImage(HttpPostedFileBase file)
        {
            string path;
            if (file != null)
            {
                string filePath = Path.Combine(HttpContext.Server.MapPath("~/Areas/Admin/UploadImg"), Path.GetFileName(file.FileName));
                file.SaveAs(filePath);
                path = "/Areas/../UploadImg/" + Path.GetFileName(file.FileName);
                return Json(new { code = 0, msg = "成功", data = new { src = path } });
            }
            else
            {
                path = "/Areas/../UploadImg";
                return Json(new { code = 1, msg = "抱歉,上传失败", data = new { src = path } });
            }
        }
        /// <summary>
        /// 后台添加文章操作
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult AdminInsertPage()
        {
            SortList[] Sort;
            //获取分类标记数据
            using (var conn = new RDBMS.SortList())
            {
                if (conn.TrySelectSortList(out Sort, null))
                {

                    ViewData["SortModel"] = Sort.ToList();
                }
            }
            return View();
        }
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult AdminInsert(Novel model)
        {
            model.CreateTime = DateTime.Now;
            model.CreateUser = CurrentUser.UserID;
            model.IsAdminBan = true;
            model.AgreeNum = 0;
            model.DisagreeNum = 0;
            model.ClickNum = 0;
            var models = new Novel[] { model };
            using (var conn = new RDBMS.Novel())
            {
                if (conn.TryInsertNovel(out bool result, models))
                {
                    if (result)
                    {
                        return Json(new { code = 0, msg = "发布成功" });
                    }
                }
            }
            return Json(new { code = 1, msg = "发布失败，请稍后重试！" });
        }
        /// <summary>
        /// 文章删除接口
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AdminDelete(int[] ids)
        {
            if (ids != null)
            {

                using (var conn = new RDBMS.Novel())
                {
                    if (conn.TryDeleteNovel(out bool userResult, ids))
                    {
                        if (userResult)
                        {
                            return Json(new { code = 0, msg = "删除成功！" });
                        }
                    }
                }

            }
            return Json(new { code = 1, msg = "删除失败，请稍后重试！" });
        }
        /// <summary>
        /// 文章设置置顶
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AdminSetTop(int ids, bool result)
        {
            using (var conn = new RDBMS.Novel())
            {
                if (conn.TryNovelTop(out bool userResult, ids, result))
                {
                    if (userResult)
                    {
                        return Json(new { code = 0, msg = "置顶成功！" });
                    }
                }
            }
            return Json(new { code = 1, msg = "置顶失败，请稍后重试！" });
        }
        /// <summary>
        /// 文章设置删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AdminSetDelete(int ids, bool result)
        {
            using (var conn = new RDBMS.Novel())
            {
                if (conn.TryNovelBan(out bool userResult, ids, IsAdmin: false, IsBan: result))
                {
                    if (userResult)
                    {
                        return Json(new { code = 0, msg = "设置禁止成功！" });
                    }
                }
            }
            return Json(new { code = 1, msg = "设置禁止失败，请稍后重试！" });
        }
        /// <summary>
        /// 文章设置审核是否通过
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult NovelIsReview(int ids,bool result)
        {
            bool c = result ? false : true;
            using (var conn = new RDBMS.Novel())
            {
                if (conn.TryNovelIsReview(out bool userResult,ids,IsReview: c))
                {
                    if (userResult)
                    {
                        return Json(new { code = 0, msg = "审核成功！" });
                    }
                    else
                    {
                        return Json(new { code = 1, msg = "审核失败，已审核！" });
                    }
                }
            }
            return Json(new { code = 1, msg = "审核失败，请稍后重试！" });
        }
    }
}